package com.moorecat.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.moorecat.MooerApplication;
import com.moorecat.R;
import com.moorecat.bean.ShopActivity;
import com.moorecat.http.HttpUtils;
import com.moorecat.utils.ui.ProgressLinearLayout;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-22 上午11:14:10 类说明
 */
public class ActivityNew extends Activity {

	private static final String TAG = "ActivityNew";

	private ListView listView;

	private ProgressLinearLayout footer;

	private ActivityListAdapter adapter;

	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	private MooerApplication mooerApp;

	private long taskId;

	private AsyncTask bitmapTask;

	private boolean isCancelled;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		mooerApp = (MooerApplication) getApplication();
		listView = (ListView) findViewById(R.id.listview);
		footer = (ProgressLinearLayout) LayoutInflater.from(this).inflate(
				R.layout.progress_small, null);
		footer.initView();
		listView.addFooterView(footer);
		adapter = new ActivityListAdapter(this, list, true);
		listView.setAdapter(adapter);
		new ActivityNewTask().execute("");
	}

	public class ActivityNewTask extends
			AsyncTask<String, Integer, List<ShopActivity>> {

		private boolean isException;

		@Override
		protected List<ShopActivity> doInBackground(String... params) {
			List<ShopActivity> list = null;
			try {
				list = HttpUtils.fetchShopActivityList(1, 0, 10);
			} catch (Exception e) {
				isException = true;
				e.printStackTrace();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<ShopActivity> result) {
			super.onPostExecute(result);

			if (isException) {
				footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
						View.VISIBLE);
			} else {
				if (result.size() > 0) {
					for (ShopActivity shopActivity : result) {
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("date", shopActivity.getDate());
						map.put("join", shopActivity.getJoin());
						map.put("repeat", shopActivity.getReply());
						map.put("require", shopActivity.getContent());
						map.put("name", shopActivity.getSummary());
						map.put("activityId", shopActivity.getId());
						map.put("icon", mooerApp.getActivityIcon());
						map.put("picPath", shopActivity.getSmallImage());
						map.put("picPath2", shopActivity.getBigImage());
						list.add(map);
					}
					listView.removeFooterView(footer);
					adapter.notifyDataSetChanged();
					bitmapTask = new BitmapTask().execute("");
				} else {
					footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
							View.GONE);
					footer.setText(null, "当前暂无活动");
				}
			}
		}

	}

	private class BitmapTask extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			long id = System.currentTimeMillis();
			taskId = id;
			// 增加一个缓存List避免列表长度变化时遍历发生错误
			List<Map<String, Object>> cacheList = new ArrayList<Map<String, Object>>();
			cacheList.addAll(list);
			Iterator<Map<String, Object>> it = cacheList.iterator();
			while (it.hasNext()) {
				/*
				 * 避免重复下载,如果有新的线程,则停止老的线程
				 */
				if (id != taskId || isCancelled) {
					break;
				}
				Map<String, Object> map = it.next();
				Bitmap icon = null;
				try {
					if (map.get("icon") == mooerApp.getActivityIcon()) {
						icon = HttpUtils.downloadBitmap((String) map.get("picPath"));
						if (icon != null) {
							map.put("icon", icon);
							publishProgress(0);
						}
					}
				} catch (Exception e) {

				}
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			adapter.notifyDataSetChanged();
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (bitmapTask != null) {
			boolean isSuccess = bitmapTask.cancel(true);
			Log.i(TAG, "BitmapTask cancelled " + isSuccess);
			isCancelled = true;
		}
	}
}
